package com.bjpowernode.javase.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;

/*
测试contains、remove方法。
    总结：放在集合中的元素需要重写equals()方法。


 */
public class CollectionTest05 {
    public static void main(String[] args) {
        Collection c = new ArrayList();

        User u1 = new User("jack");
        User u2 = new User("jack");

        c.add(u1);

       /* //User没有重写toString方法之前
        System.out.println(c.contains(u2));//false*/

        //有重写toString方法之后
        System.out.println(c.contains(u2));//true


        Integer x = new Integer(10000);
        c.add(x);

        Integer y = new Integer(10000);
        System.out.println(c.contains(y));//true,因为Integer的equals方法重写了。

        Collection cc = new ArrayList();
        String s1 = new String("hello");
        cc.add(s1);
        String s2 = new String("hello");
        cc.remove(s2);
        System.out.println(cc.size());//0

    }
}
class User{
    private String name;
    public User(){}
    public User(String name){this.name = name;}
    //重写equals方法
    @Override
   public boolean equals(Object o){
        if(o == null || !(o instanceof User)) return false;
        if(o==this) return true;
        User user = (User) o;
        return user.name.equals(this.name);
    }

}
